import { http } from "@/utils/http";
import { formatToken } from "@/utils/auth";

export interface ApiResponse<T> {
  code: number;
  success: boolean;
  msg: string;
  data: T;
}

export interface IdentityInfo {
  id?: number;
  type: string;
  status: number;
  org_id?: number | null;
  org_name?: string | null;
  region_id?: number | null;
  region_name?: string | null;
  verified_at?: string | null;
}

export interface ProfileInfo {
  id: number;
  phone: string;
  nickname: string;
  real_name?: string;
  avatar?: string;
  status?: number;
}

export interface LoginSuccessData {
  accessToken: string;
  refreshToken: string;
  expires: string;
  username: string;
  nickname: string;
  roles: Array<string>;
  permissions: Array<string>;
  menus: Array<any>;
  profile: ProfileInfo;
  identities: Array<IdentityInfo>;
  client: string;
  partial: false;
}

export interface PartialAuthData {
  accessToken: string;
  expires: string;
  partial: true;
  incompleteAction: string;
  context: Record<string, any>;
}

export type LoginResponse = LoginSuccessData | PartialAuthData;

export interface RefreshTokenData {
  accessToken: string;
  refreshToken: string;
  expires: string;
}

export type LoginApiResult = ApiResponse<LoginResponse>;
export type RefreshTokenResult = ApiResponse<RefreshTokenData>;

export interface RegisterRequest {
  phone: string;
  password?: string;
  code?: string;
  nickname?: string;
  real_name?: string;
  identities?: Array<string> | string;
  identity_type?: string;
  register_channel?: string;
  partial_token?: string;
  partialToken?: string;
  region_id?: number | null;
  org_id?: number | null;
  invited_by?: number;
  invite_code?: string;
}

export interface PartialStatusData {
  context: Record<string, any>;
  identities: Array<IdentityInfo>;
  company?: Record<string, any> | null;
}

/** 登录 */
export const postLogin = (data: { phone: string; password: string }) => {
  return http.request<LoginApiResult>("post", "/auth/login", {
    data: {
      type: "password",
      credentials: data
    }
  });
};

export const postSmsLogin = (data: { phone: string; code: string }) => {
  return http.request<LoginApiResult>("post", "/auth/login", {
    data: {
      type: "sms",
      credentials: data
    }
  });
};

export const postPlatformLogin = (data: {
  ticket: string;
  nickname?: string;
  avatar?: string;
  client?: string;
  openid?: string;
}) => {
  const payload = {
    type: "pltform",
    credentials: {
      ticket: data.ticket,
      nickname: data.nickname,
      avatar: data.avatar,
      openid: data.openid
    }
  };

  return http.request<LoginApiResult>("post", "/auth/login", {
    data: payload,
    headers: {
      "X-Client": data.client ?? "buyer"
    }
  });
};

/** 注册或绑定手机号 */
export const postRegister = (data: RegisterRequest) => {
  const payload = Object.fromEntries(
    Object.entries(data).filter(([, value]) => value !== undefined)
  );
  return http.request<LoginApiResult>("post", "/auth/register", {
    data: payload
  });
};

/** 刷新`token` */
export const refreshTokenApi = (data?: object) => {
  return http.request<RefreshTokenResult>("post", "/auth/refresh", { data });
};

export const getPartialStatus = (token: string) => {
  return http.request<ApiResponse<PartialStatusData>>(
    "get",
    "/auth/partial/status",
    {},
    {
      headers: { Authorization: formatToken(token) }
    }
  );
};
